package com.tzy.shop.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * mybaits plus 自动生成代码配置
 */
@Configuration
public class MybatisGeneratorConfig {
	public static void main(String[] args) {
		//项目的当前的路径
		String projectPath = System.getProperty("user.dir");
		Map<OutputFile,String> paths = new HashMap<>();
//		paths.put(OutputFile.entity,projectPath.concat("/common/src/main/java/com/tzy/common/entity"));
//		paths.put(OutputFile.mapper,projectPath.concat("/shop/src/main/java/com/tzy/shop/mapper"));
//		paths.put(OutputFile.mapperXml,projectPath.concat("/shop/src/main/java/com/tzy/shop/mapper"));
//		paths.put(OutputFile.serviceImpl,projectPath.concat("/shop/src/main/java/com/tzy/shop/service"));
//		paths.put(OutputFile.service,projectPath.concat("/shop/src/main/java/com/tzy/shop/service"));
//		paths.put(OutputFile.controller,projectPath.concat("/shop/src/main/java/com/tzy/shop/controller"));
		FastAutoGenerator.create("jdbc:mysql://123.60.90.103:3306/take_out", "root", "Tzy_123456")
				.globalConfig(builder -> {
					builder.author("ljh") // 设置作者
							.fileOverride() // 覆盖已生成文件
							.disableOpenDir()
							.outputDir(projectPath); // 指定输出目录
				})
				.packageConfig(builder -> {
					builder.parent("com.tzy.shop") // 设置父包名
//                            .moduleName("") // 设置父包模块名
							.serviceImpl("service")
							.pathInfo(paths); // 设置mapperXml生成路径
				})
				.strategyConfig(builder -> {
					builder.addInclude("shop") // 设置需要生成的表名
							.controllerBuilder().enableRestStyle()//配置restController
							.mapperBuilder().enableBaseResultMap().enableBaseColumnList()
							.serviceBuilder().formatServiceImplFileName("%sService");

				})
				.templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板，默认的是Velocity引擎模板
				.execute();
	}

	// 最新版
//	@Bean
//	public MybatisPlusInterceptor mybatisPlusInterceptor() {
//		MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
//		interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
//		return interceptor;
//	}
}